//导入页面所需要的 CSS
import "../../css/adminlte.min.css";
//导入封装后的 实例
import req from "../../utils/request";

//获取表单元素
let loginForm = document.querySelector("#loginForm");
let inputs = document.querySelectorAll("input");
//为 form 绑定提交事件. 当表单在提交时触发
loginForm.onsubmit = async function (e) {
  //阻止默认行为
  e.preventDefault();
  //获取表单元素的值
  let username = inputs[0].value;
  let pwd = inputs[1].value;
  //发送 AJAX 请求    post  http://127.0.0.1:8081/user/login
  let result = await req.post("/user/login", {
    username,
    pwd,
  });

  //成功的情况 持久化保存 token
  localStorage.setItem("token", result);
  //跳转到用户列表页面
  location.href = "/users.html";
};
